package com.doit.day01;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.PartitionInfo;

import java.util.List;
import java.util.Map;
import java.util.Random;

public class MyPartitioner implements Partitioner {

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        //这个方法里面可以写自己想要的分区逻辑，返回值就是具体数据会被发送到哪一个分区
        int code = Math.abs(value.toString().hashCode());
        List<PartitionInfo> partitionInfos = cluster.partitionsForTopic(topic);
        for (PartitionInfo partitionInfo : partitionInfos) {
            int partition = partitionInfo.partition();
            Node leader = partitionInfo.leader();
        }
        int numPartition = partitionInfos.size();
        return new Random().nextInt(partitionInfos.size());
    }
    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
